<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;


class Index extends Controller
{
    /**
     * 后台登录页面
     * @return \think\response\View
     */
    public function index()
    {
        return view('admin@index/login');
    }

    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function logindo(Request $request)
    {
        $p=$request->post();

//        var_dump($p);
        $name = $p['username'];
        $pass = md5($p['userpass']);
        $user=new \app\admin\model\Con1();
        $res = $user->where(['username'=>$name,'userpass'=>$pass])->find();
//        var_dump($res);
//        die;
        if($res==null){
            $this->error('用户名或密码错误');

        }
        $result['username']=$res['username'];
        $result['userpass']=$res['userpass'];
//        存入session
        session('user', $result);
//        $res=Session::get('user');
//        var_dump($_SESSION);
//        die;
        //通过
        $res=Db::table('lamp_user')->field('id')->where(['username'=>$res['username']])->select();

        $id = array();
        foreach ($res as $v)
        {
            $id = $v['id'];
        }
//        $myqx = Db::table('lamp_user')
//            ->alias('lu')
//            ->join('lamp_user_role lur','lu.id = lur.uid')
//            ->join('lamp_role lr','lur.rid = lr.id')
//            ->join('lamp_role_node lrn','lrn.rid = lr.id')
//            ->join('lamp_node ln','ln.id = lrn.nid')
//            ->where('lu.id',$id)
//            ->select();
//        foreach ($myqx as $v)
//        {
//            $list[]=$v['id'];
//        }
//
//
//        var_dump($list);die;

//        die;
        $res=Db::table('lamp_user')
            ->alias('lu')
            ->join('lamp_user_role lur','lu.id= lur.uid')

            ->join('lamp_role lr','lr.id=lur.rid')
            ->where('lu.id',$id)
            ->select();
//        var_dump($res);
//        die;
        //获取到权限表中的id 通过这个id去查
        $roleid = array();
        foreach ($res as $v)
        {
            $roleid[] = $v['rid'];

        }
//        var_dump($roleid);die;
        //通过roleid去查询到对应的
//        $result = Db::table('lamp_role_node')->field(['nid'])->select($roleid);

        foreach($roleid as $v){

        $result = Db::table('lamp_role_node')->alias('rn')
            ->join('lamp_node n','n.id=rn.nid')
            ->where('rn.rid',$v)
            ->select();
            $arrr[]=$result;
        }
//    var_dump($arrr);die;
    //将三维数组合并
        $node = $arrr[0];
        for ($i = 1; $i < count($arrr); $i++) {
            $node = array_merge($node, $arrr[$i]);
        }
//        var_dump($node);die;

//        $res = Db::table('lamp_node')->field(['id','mname', 'aname'])->select($node);
//        var_dump($res);die;
        $list = array();
        foreach ($node as $v)
        {
            $list[$v['mname']][] = $v['aname'];
            if($v['aname']=="edit"){
                $list[$v['mname']][]="update";
            }
            if($v['aname']=="create"){
                $list[$v['mname']][]="save";
            }
            if($v['aname']=="rolelist"){
                $list[$v['mname']][]="saverole";
            }

            if($v['aname']=="rolediv"){
                $list[$v['mname']][]="saverole";
            }
//            if($v['aname']=="create"){
//                $nodelist[$v['mname']][]="save";
//            }
        }
//        var_dump($list);die;
        session('nodelist', $list);
//        var_dump(session('user'));die;

        $this->redirect('admin/Main/index');
    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {
//        session(null);
        unset($_SESSION);
        $this->redirect('admin/Index/index');
    }

}
